#include "chooselevelscene.h"
#include<QMenu>
#include<QMenuBar>
#include<QAction>
#include<QPainter>
#include<QPixmap>
#include"mypushbutton.h"
#include<QDebug>
#include<QTimer>
#include<QLabel>
#include<QSoundEffect>

ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow{parent}
{
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("选择关卡场景");

    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);

    QMenu* startMenu=bar->addMenu("开始");
    QAction* quitAction= startMenu->addAction("退出");

    QSoundEffect *chooseSound=new QSoundEffect(this);
    chooseSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));

    QSoundEffect *backSound=new QSoundEffect(this);
    backSound->setSource(QUrl::fromLocalFile(":/res/BackButtonSound.wav"));

    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    MyPushButton* backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height()-20);

    connect(backBtn,&MyPushButton::clicked,[=](){
        //qDebug()<<"返回按钮被点击";
        //返回主场景
        backSound->play();

        QTimer::singleShot(500,this,[=](){
            emit this->chooseSceneBack();
        });
    });

    //创建选择关卡按钮
    for(int i=0;i<20;i++){
        MyPushButton* levelBtn=new MyPushButton(":/res/LevelIcon.png");
        levelBtn->setParent(this);
        levelBtn->move((i%4)*70+25,(i/4)*70+130);
        connect(levelBtn,&MyPushButton::clicked,[=](){
            //qDebug() <<"点击的关卡是"<<i;
            chooseSound->play();

            this->hide();
            play=new PlayScene(i+1);
            play->setGeometry(this->geometry());
            play->show();

            connect(play,&PlayScene::chooseSceneBack,[=](){
                this->setGeometry(play->geometry());
                this->show();;
                delete play;
                play=NULL;
            });
        });

        QLabel* levelLabel=new QLabel();
        levelLabel->setParent(this);
        levelLabel->setFixedSize(levelBtn->width(),levelBtn->height());
        levelLabel->setText(QString::number(i+1));
        levelLabel->move((i%4)*70+25,(i/4)*70+130);

        //设置文字在Label中的对齐方式
        levelLabel->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        levelLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}

void ChooseLevelScene::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);

}
